<template>
  <div class="dashboard-home">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card class="welcome-card">
          <div class="welcome-header">
            <h2>欢迎回来，{{ user?.username }}</h2>
            <p>欢迎使用知识图谱威胁情报分析系统</p>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" class="feature-row">
      <el-col :span="6">
        <el-card class="feature-card" @click="navigateTo('/dashboard/intel')">
          <div class="feature-icon">
            <el-icon><el-icon-document /></el-icon>
          </div>
          <div class="feature-title">威胁情报浏览</div>
          <div class="feature-desc">浏览、搜索和筛选各种威胁情报信息</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="feature-card" @click="navigateTo('/dashboard/extract')">
          <div class="feature-icon">
            <el-icon><el-icon-position /></el-icon>
          </div>
          <div class="feature-title">情报抽取</div>
          <div class="feature-desc">从非结构化文本中抽取威胁情报</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="feature-card" @click="navigateTo('/dashboard/graph')">
          <div class="feature-icon">
            <el-icon><el-icon-connection /></el-icon>
          </div>
          <div class="feature-title">知识图谱</div>
          <div class="feature-desc">可视化图谱展示威胁实体与关系</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="feature-card" @click="navigateTo('/dashboard/trust')">
          <div class="feature-icon">
            <el-icon><el-icon-checked /></el-icon>
          </div>
          <div class="feature-title">可信度评估</div>
          <div class="feature-desc">分析评估威胁情报的可信度</div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" class="stats-row">
      <el-col :span="8">
        <el-card class="stat-card" :class="{ 'loading-card': isLoading }">
          <template #header>
            <div class="stat-header">
              <el-icon><el-icon-info-filled /></el-icon>
              <span>威胁情报总数</span>
            </div>
          </template>
          <div class="stat-value">{{ threatIntelCountValue }}</div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="stat-card" :class="{ 'loading-card': isLoading }">
          <template #header>
            <div class="stat-header">
              <el-icon><el-icon-data-analysis /></el-icon>
              <span>实体节点总数</span>
            </div>
          </template>
          <div class="stat-value">{{ entityNodeCountValue }}</div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="stat-card" :class="{ 'loading-card': isLoading }">
          <template #header>
            <div class="stat-header">
              <el-icon><el-icon-link /></el-icon>
              <span>关系数量</span>
            </div>
          </template>
          <div class="stat-value">{{ relationshipCountValue }}</div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, inject } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../../stores/auth'
import { storeToRefs } from 'pinia'
import axios from 'axios'
import { 
  Document as ElIconDocument,
  Position as ElIconPosition,
  Connection as ElIconConnection,
  CircleCheckFilled as ElIconChecked,
  InfoFilled as ElIconInfoFilled,
  DataAnalysis as ElIconDataAnalysis,
  Link as ElIconLink
} from '@element-plus/icons-vue'

// 定义props
const props = defineProps({
  threatIntelCount: {
    type: [String, Number],
    default: '1'
  },
  entityNodeCount: {
    type: [String, Number],
    default: '5'
  },
  relationshipCount: {
    type: [String, Number],
    default: '3'
  },
  loading: {
    type: Boolean,
    default: false
  }
})

const router = useRouter()
const authStore = useAuthStore()
const { user } = storeToRefs(authStore)

// 获取注入的统计数据
const dashboardStats = inject('dashboardStats', null)

// 计算要显示的值，优先使用props，如果没有props则使用inject的值
const threatIntelCountValue = computed(() => props.threatIntelCount || (dashboardStats ? dashboardStats.threatIntelCount.value : '加载中...'))
const entityNodeCountValue = computed(() => props.entityNodeCount || (dashboardStats ? dashboardStats.entityNodeCount.value : '加载中...'))
const relationshipCountValue = computed(() => props.relationshipCount || (dashboardStats ? dashboardStats.relationshipCount.value : '加载中...'))
const isLoading = computed(() => props.loading || (dashboardStats ? dashboardStats.loading.value : true))

const navigateTo = (path) => {
  router.push(path)
}
</script>

<style scoped>
.dashboard-home {
  padding: 10px 0;
}

.welcome-card {
  margin-bottom: 20px;
  border-radius: 8px;
}

.welcome-header {
  text-align: center;
  padding: 10px 0;
}

.welcome-header h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.welcome-header p {
  margin: 10px 0 0;
  color: #606266;
}

.feature-row {
  margin-bottom: 20px;
}

.feature-card {
  height: 180px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.feature-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
}

.feature-icon {
  font-size: 36px;
  margin-bottom: 15px;
  color: #409eff;
}

.feature-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 8px;
  color: #303133;
}

.feature-desc {
  font-size: 13px;
  color: #909399;
  padding: 0 15px;
}

.stats-row {
  margin-bottom: 20px;
}

.stat-card {
  border-radius: 8px;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
}

.loading-card {
  position: relative;
  overflow: hidden;
}

.loading-card::after {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.5), transparent);
  animation: loading-shine 1.5s infinite;
}

@keyframes loading-shine {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

.stat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: #606266;
}

.stat-value {
  text-align: center;
  font-size: 32px;
  font-weight: bold;
  color: #409eff;
  padding: 15px 0;
}
</style> 